package com.cogent.common.date;

import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.opensymphony.oscache.util.StringUtil;

public class JodaDateUtil {
	static {
		System.setProperty("user.timezone","GMT+8");
	}
	
	public static String getCurDate() {
		DateTime dt = new DateTime();
		
		return dt.toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS"));
	}
	
	public static String getCurDate(String format) {
		DateTime dt = new DateTime();
		
		return dt.toString(DateTimeFormat.forPattern(format));
	}
	
	public static int compareDate(String date1, String date2) {
		DateTime dt1 = new DateTime(stringToDate(date1, getFormate(date1)));
		DateTime dt2 = new DateTime(stringToDate(date2, getFormate(date2)));
		
		return dt1.compareTo(dt2);
	}
	
	private static String getFormate(String date) {
		String format = "yyyy-MM-dd";
		if((date.length() > 10) && (date.length() < 20))
			format = "yyyy-MM-dd HH:mm:ss";
		else if(date.length() > 19)
			format = "yyyy-MM-dd HH:mm:ss.SSS";
		
		return format;			
	}
	
	public static Date getCurSystemDate() {
		DateTime dt = new DateTime();
		
		return dt.toDate();
	}
	
	public static Date getCurSystemDate(String format) {
		String str = getCurDate(format);
		
		return stringToDate(str, format);
	}
	
	public static Date stringToDate(String str, String format) {
		if(str.length() > format.length())
			str = str.substring(0, format.length());
		DateTimeFormatter fmt = DateTimeFormat.forPattern(format);
		DateTime dt = null;
		try{
			fmt = fmt.withZone(DateTimeZone.forID("Asia/Shanghai"));
			dt = fmt.parseDateTime(str);
		}catch(Exception ex) {
			fmt = fmt.withZone(DateTimeZone.UTC);
			dt = fmt.parseDateTime(str);
		}
		
		return dt.toDate();
	}
	
	public static Date stringToDate(String str) {
		return stringToDate(str, getFormate(str));
	}
	
	public static boolean isGreater(String date1, String date2) {
		if(date1.length() < 1)
			return false;
		
		int val = compareDate(date1, date2);
		if(val < 1)
			return false;
		else
			return true;
	}
	
	public static String formatDateToString(String date, String format) {
		if(date.length() < 1)
			return "";
		
		if(date.length() > format.length()) {
			if(format.length() == 10)
				date = date.substring(0, date.indexOf(" "));
			else
				date = date.substring(0, format.length());
		}
		
		DateTimeFormatter fmt = DateTimeFormat.forPattern(format);
		DateTime dt = null;
		try{
			fmt = fmt.withZone(DateTimeZone.forID("Asia/Shanghai"));
			dt = fmt.parseDateTime(date);
		}catch(Exception ex) {
			fmt = fmt.withZone(DateTimeZone.UTC);
			dt = fmt.parseDateTime(date);
		}
		
		return dt.toString(DateTimeFormat.forPattern(format));
	}
	
	public static String dateToString(Date date, String format) {
		DateTime dt = new DateTime(date);
		
		return dt.toString(DateTimeFormat.forPattern(format));
	}
	
	public static String addYear(String str, int day) {
		String format = getFormate(str);
		DateTime d1 = new DateTime(stringToDate(str, format));
		
		return d1.plusYears(day).toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
	}
	
	public static String addDay(String str, int day) {
		String format = getFormate(str);
		DateTime d1 = new DateTime(stringToDate(str, format));
		
		return d1.plusDays(day).toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
	}
	
	public static String addHour(String str, int hour) {
		String format = getFormate(str);
		DateTime d1 = new DateTime(stringToDate(str, format));
		
		return d1.plusHours(hour).toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
	}
	
	public static int getYear(Date date) {
		DateTime dt = new DateTime(date);
		
		return dt.getYear();
	}
	
	public static int getCurYear() {
		DateTime dt = new DateTime();
		
		return dt.getYear();
	}
	
	public static int getMonth(Date date) {
		DateTime dt = new DateTime(date);
		
		return dt.getMonthOfYear();
	}
	
	public static int getDay(Date date) {
		DateTime dt = new DateTime(date);
		
		return dt.getDayOfMonth();
	}
	
	public static String getFirstDateOfWeek() {
		DateTime dt = new DateTime();
		LocalDate local = dt.toLocalDate().dayOfWeek().setCopy(1);
		
		return local.toString(DateTimeFormat.forPattern("yyyy-MM-dd"));
	}
	
	public static String getEndDateOfWeek() {
		DateTime dt = new DateTime();
		LocalDate local = dt.toLocalDate().dayOfWeek().setCopy(7).plusDays(1);
		
		return local.toString(DateTimeFormat.forPattern("yyyy-MM-dd"));
	}
	
	public static String getDatePlusSecond(int second) {
		DateTime dt = new DateTime();
		dt = dt.plusSeconds(second);
		
		return dt.toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
	}
	
	public static String getDatePlusMinute(int minute) {
		DateTime dt = new DateTime();
		dt = dt.plusMinutes(minute);
		
		return dt.toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
	}
	
	public static String getDatePlusHour(int hour) {
		DateTime dt = new DateTime();
		dt = dt.plusHours(hour);
		
		return dt.toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
	}
	
	public static String getDatePlusDay(int day) {
		DateTime dt = new DateTime();
		dt = dt.plusDays(day);
		
		return dt.toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
	}
	
	public static String getFirstDateOfMonth() {
		DateTime dt = new DateTime();
		LocalDate local = dt.toLocalDate().dayOfMonth().setCopy(1);
		
		return local.toString(DateTimeFormat.forPattern("yyyy-MM-dd"));
	}
	
	public static String getEndDateOfMonth() {
		DateTime dt = new DateTime();
		LocalDateTime local = dt.toLocalDateTime().monthOfYear().setCopy(dt.getMonthOfYear() + 1)
			.dayOfMonth().setCopy(1).minusDays(1).hourOfDay().setCopy(23)
			.minuteOfHour().setCopy(59).secondOfMinute().setCopy(59);
		
		return local.toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
	}
	
	public static int getAge(String date) {
		int age = 1;
		DateTimeFormatter fmt = DateTimeFormat.forPattern(getFormate(date));
		DateTime dt = null;
		try{
			fmt = fmt.withZone(DateTimeZone.forID("Asia/Shanghai"));
			dt = fmt.parseDateTime(date);
		}catch(Exception ex) {
			fmt = fmt.withZone(DateTimeZone.UTC);
			dt = fmt.parseDateTime(date);
		}
		
		int curYear = getCurYear();
		if(dt.getYear() < curYear)
			age = curYear - dt.getYear();
		
		return age;
	}
	
	public static String getLongDate(String date) {
		return formatDateToString(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	public static String getShortDate(String date) {
		return formatDateToString(date, "yyyy-MM-dd");
	}
	
	public static String toOracleQueryDate(String date) {
		return "to_date('" + formatDateToString(date, "yyyy-MM-dd HH:mm:ss") + "', 'yyyy-MM-dd hh24:mi:ss')";
	}
	
	public static String getLasetDayTime(String date) {
		if(StringUtil.isEmpty(date))
			return "";
		
		return date + " 23:59:59";
	}
}
